package icu.decade.store.utils;

import icu.decade.store.dto.Result;

import java.util.Date;
import java.util.Optional;

/**
 * @author qk
 * @version 1.0
 * @className RUtil
 * @description 结果处理工具类
 * @date 2023/6/25 15:01
 **/
public class RUtil {

    public static final String SUCCESS_CODE = "0";
    public static final String SUCCESS_LABEL = "success";
    public static final String FAIL_CODE = "-1";
    public static final String FAIL_LABEL = "fail";

    public static  <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, SUCCESS_LABEL, new Date(), data);
    }

    public static Result success() {
        return success(null);
    }

    public static Result fail(String msg) {
        return new Result<>(FAIL_CODE, msg, new Date(), null);
    }

    public static Result fail() {
        return fail(FAIL_LABEL);
    }

    public static boolean isSuccess(Result result) {
        return Optional.ofNullable(result).map(Result::getData).filter(SUCCESS_CODE::equals).isPresent();
    }

    public static boolean isFail(Result result) {
        return !isSuccess(result);
    }

    /**
     *
     * @param result
     * @return data数据是否存在 存在true or false
     */
    public static boolean checkData(Result result) {
        return Optional.ofNullable(result).map(Result::getData).isPresent();
    }
}
